//登录
function wxLogin(func) {
	// console.log(this)
	//调用登录接口
	//1.小程序调用wx.login得到code.
	var _this = this;
	wx.login({
		success: function (res) {
			var code = res['code'];
			// console.log(code)
			//2.小程序调用wx.getUserInfo得到rawData, signatrue, encryptData.
			wx.getUserInfo({
				success: function (info) {
					// console.log(info);
					var rawData = info['rawData'];
					var signature = info['signature'];
					var encryptedData = info['encryptedData']; //注意是encryptedData不是encryptData...坑啊
					var iv = info['iv'];
					wx.setStorage({    //session3rd存入微信缓存
            key: "userInfo",
            data: info.userInfo,
						success: function () {
							// console.log('个人信息存入缓存')
						}
					})
					//3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
					wx.request({
						url: 'https://wemall.17link.cc/post/login',
            method:'POST',
						data: {
							"code": code,
							"rawData": rawData,
              // "iv": iv,
              // "encryptedData": encryptedData,
              // "signature": signature
						},
						success: function (res) {
							// console.log(res)
							if (res.statusCode != 200) {
								wx.showModal({
									title: '登录失败'
								});
							} else {
								var session = res.data.data;
								wx.setStorage({    //session3rd存入微信缓存
									key: "3rd_session",
									data: session,
									success: function () {
										// console.log('session3rd存入缓存')
									}
								})

							}
							typeof func == "function" && func(res.data);
						}
					});
				}
			});
		}
	});
}

module.exports = {
	wxLogin
}